package keshe.view;

import keshe.ACCOUNT;
import keshe.Info;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDateTime;
import java.util.ArrayList;

public class AccountInfoJFrame extends JFrame implements ActionListener,Runnable{//个人账户银行细则界面

    private DefaultListModel<Info>infolm=new DefaultListModel<>();
    private JList<Info>infojl=new JList<>(infolm);
    private JScrollPane jsp=new JScrollPane(infojl);
    public AccountInfoJFrame(){
        this(new ACCOUNT());
    }
    private JPanel cmdpanel=new JPanel();
    private JTextField time01=new JTextField("2024");
    private JTextField time02=new JTextField("01");
    private JTextField time03=new JTextField("01");
    private ComboBoxModel<String> findmodel=new DefaultComboBoxModel<>(new String[]{"全部","按时间查找","按金额查找"});
    private JComboBox<String> find=new JComboBox<>(findmodel);
    private JTextField money=new JTextField("0");
    private ComboBoxModel<String> sortmodel=new DefaultComboBoxModel<>(new String[]{"按时间排序","按金额排序"});
    private JComboBox<String> sort=new JComboBox<>(sortmodel);
    private ArrayList<Info>infos;

    private JPanel mainpanel=new JPanel();

    public AccountInfoJFrame(ACCOUNT account){
        this.setLayout(null);
        this.setBackground(Color.cyan);
        this.setBounds(600,300,600,210);
        infos=account.InfoArrayList;
        mainpanel.setBounds(0,0,600,150);
        //添加滚动窗格，添加列表框
        mainpanel.setLayout(null);
        Thread thread=new Thread(this);
        thread.start();

        jsp.setBounds(0,0,600,150);
        mainpanel.add(jsp);
        infojl.setBackground(Color.cyan);

        cmdpanel.setBounds(0,150,600,30);
        cmdpanel.setLayout(null);
        cmdpanel.setBackground(Color.cyan);
        AddButton();
        AddListener();

        this.getContentPane().add(mainpanel);
        this.getContentPane().add(cmdpanel);
        this.setVisible(true);
    }

    private void AddListener() {
        find.addActionListener(e -> {
            if(find.getSelectedIndex()==0){
                Thread thread=new Thread(new Runnable() {
                    @Override
                    public void run() {
                        infolm.removeAllElements();
                        for (Info info:infos){
                            infolm.addElement(info);
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException ex) {
                                throw new RuntimeException(ex);
                            }
                        }
                    }
                });
                thread.start();
            }
            if(find.getSelectedIndex()==1) {
                Thread thread=new Thread(new Runnable() {
                    @Override
                    public void run() {
                        infolm.removeAllElements();
                        for (Info info : infos) {
                            LocalDateTime time = info.getTime();
                            if (time.getYear() == Integer.parseInt(time01.getText()) && time.getMonthValue() == Integer.parseInt(time02.getText()) && time.getDayOfMonth() == Integer.parseInt(time03.getText())) {
                                infolm.addElement(info);
                                try {
                                    Thread.sleep(500);
                                } catch (InterruptedException ex) {
                                    throw new RuntimeException(ex);
                                }
                            }
                        }
                    }
                });
                thread.start();
            }
            if(find.getSelectedIndex()==2){
                Thread thread=new Thread(new Runnable() {
                    @Override
                    public void run() {
                        infolm.removeAllElements();
                        for (Info info:infos){
                            if(info.getMoney()==Double.parseDouble(money.getText())){
                                infolm.addElement(info);
                                try {
                                    Thread.sleep(500);
                                } catch (InterruptedException ex) {
                                    throw new RuntimeException(ex);
                                }
                            }
                        }
                    }
                });
                thread.start();
            }
        });
        sort.addActionListener(e->{
            Thread t1=new Thread(new Runnable() {
                @Override
                public void run() {
                    for(int i=0;i<infolm.getSize()-1;i++){
                        for(int j=i+1;j<infolm.getSize();j++){
                            if(infolm.get(i).getTime().isBefore(infolm.get(j).getTime())){
                                Info temp=infolm.get(i);
                                infolm.set(i,infolm.get(j));
                                infolm.set(j,temp);
                                try {
                                    Thread.sleep(500);
                                } catch (InterruptedException ex) {
                                    throw new RuntimeException(ex);
                                }
                            }
                        }
                    }
                }
            });
            Thread t2=new Thread(new Runnable() {
                @Override
                public void run() {
                    for(int i=0;i<infolm.getSize()-1;i++){
                        for(int j=i+1;j<infolm.getSize();j++){
                            if(infolm.get(i).getMoney()<infolm.get(j).getMoney()){
                                Info temp=infolm.get(i);
                                infolm.set(i,infolm.get(j));
                                infolm.set(j,temp);
                                try {
                                    Thread.sleep(500);
                                } catch (InterruptedException ex) {
                                    throw new RuntimeException(ex);
                                }
                            }
                        }
                    }
                }
            });
            if(sort.getSelectedIndex()==0){
                t1.start();
            }
            if(sort.getSelectedIndex()==1){
                t2.start();
            }
        });
    }

    private void AddButton() {
        time01.setBounds(5,0,35,20);
        JLabel Time01=new JLabel("年");
        Time01.setBounds(40,0,15,20);
        time02.setBounds(55,0,20,20);
        JLabel Time02=new JLabel("月");
        Time02.setBounds(75,0,15,20);
        time03.setBounds(90,0,20,20);
        JLabel Time03=new JLabel("日");
        Time03.setBounds(110,0,15,20);

        cmdpanel.add(time01);
        cmdpanel.add(time02);
        cmdpanel.add(time03);

        cmdpanel.add(Time01);
        cmdpanel.add(Time02);
        cmdpanel.add(Time03);
        find.setBounds(260,0,100,20);
        money.setBounds(130,0,100,20);
        JLabel money01=new JLabel("元");
        money01.setBounds(230,0,15,20);
        cmdpanel.add(money);
        cmdpanel.add(money01);
        sort.setBounds(380,0,100,20);
        cmdpanel.add(find);
        cmdpanel.add(sort);
    }

    public static void main(String[] args) {
        ACCOUNT account=new ACCOUNT();
        account.InfoArrayList.add(new Info(1,100,LocalDateTime.of(2021,1,1,10,10)));
        account.InfoArrayList.add(new Info(1,500,LocalDateTime.of(2021,10,10,2,10)));
        account.InfoArrayList.add(new Info(0,300,LocalDateTime.now()));
        new AccountInfoJFrame(account);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }

    @Override
    public void run() {
        for (Info info:infos){
            infolm.addElement(info);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
